package online.heycm.platform.oss.strategy;

import com.amazonaws.ClientConfiguration;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.client.builder.AwsClientBuilder;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import online.heycm.platform.oss.OssProperties;

public class MinioStrategy implements S3Strategy {

    @Override
    public AmazonS3 createAmazonS3(OssProperties properties) {
        ClientConfiguration config = new ClientConfiguration();
        config.setMaxConnections(properties.getMaxConnections());
        config.setConnectionTimeout(properties.getConnectionTimeout());
        config.setSocketTimeout(properties.getSocketTimeout());
        config.setProtocol(properties.getProtocol());
        AmazonS3ClientBuilder builder = AmazonS3Client.builder()
                .withCredentials(new AWSStaticCredentialsProvider(new BasicAWSCredentials(properties.getAccessKey(), properties.getSecretKey())))
                .withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration(properties.getEndpoint(), properties.getRegion()))
                .withPathStyleAccessEnabled(properties.isPathStyleAccess())
                .withClientConfiguration(config);
        return builder.build();
    }
}
